package net.hn.hnms.biz.energy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.energy.domain.WaterSupplyCompany;
import net.hn.hnms.biz.energy.mapper.WaterSupplyCompanyServiceMapper;
import net.hn.hnms.biz.energy.request.WaterSupplyCompanyReq;
import net.hn.hnms.biz.energy.service.WaterSupplyCompanyService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WaterSupplyCompanyServiceImpl extends BaseService<WaterSupplyCompanyReq, WaterSupplyCompany> implements WaterSupplyCompanyService {
    @Autowired
    private WaterSupplyCompanyServiceMapper waterSupplyCompanyServiceMapper;

    @Override
    public int insert(WaterSupplyCompanyReq waterSupplyCompanyReq) {
        WaterSupplyCompany insertObj = new WaterSupplyCompany();
        BeanUtil.copyProperties(waterSupplyCompanyReq, insertObj);
        return waterSupplyCompanyServiceMapper.insert(insertObj);
    }

    /**
     * @param idList 批量删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDeleteByIds(List<String> idList) {
        waterSupplyCompanyServiceMapper.updateDeleteByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(WaterSupplyCompanyReq waterSupplyCompany) {
        WaterSupplyCompany updateObj = new WaterSupplyCompany();
        BeanUtil.copyProperties(waterSupplyCompany, updateObj);
        int updateCount = waterSupplyCompanyServiceMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public WaterSupplyCompany selectDetailById(String id) {
        WaterSupplyCompany waterSupplyCompany = waterSupplyCompanyServiceMapper.selectById(id);
        if (waterSupplyCompany == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return waterSupplyCompany;
    }

    @Override
    public ResponsePage<WaterSupplyCompany> selectPageList(WaterSupplyCompanyReq executionOther, PageQuery pageQuery) {
        Page<WaterSupplyCompany> page = waterSupplyCompanyServiceMapper.selectPage(pageQuery.build(), buildQuery(executionOther).orderByDesc(WaterSupplyCompany::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public List<WaterSupplyCompany> selectList(WaterSupplyCompanyReq waterSupplyCompanyReq) {
        List<WaterSupplyCompany> list = waterSupplyCompanyServiceMapper.selectList(buildQuery(waterSupplyCompanyReq));
        return list;
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<WaterSupplyCompany> buildQuery(WaterSupplyCompanyReq req){
        return new LambdaQueryWrapper<WaterSupplyCompany>()
                .like(StrUtil.isNotBlank(req.getWaterSupplyCompanyName()), WaterSupplyCompany::getWaterSupplyCompanyName, req.getWaterSupplyCompanyName())
                .eq(StrUtil.isNotBlank(req.getMineCode()),WaterSupplyCompany::getMineCode,req.getMineCode())
                .ge(req.getBeginDateTime() != null, WaterSupplyCompany::getCreateTime, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, WaterSupplyCompany::getCreateTime, req.getEndDateTime());
    }

}
